package com.juku2024.juku.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import com.juku2024.juku.dialog.CommonDialog
import com.juku2024.juku.dialog.DialogUtils

abstract class BaseFragment: Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutView(inflater, container)
    }

    abstract fun layoutView(inflater: LayoutInflater, container: ViewGroup?): View

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        init(savedInstanceState)
        bindEvent()
    }

    /**
     * 初始化控件，比如findViewById
     */
    protected open fun initView(rootView: View) {}
    protected open fun init(savedInstanceState: Bundle?) {}
    /**
     * 绑定控件的事件，比如button的onclick事件
     */
    protected open fun bindEvent() {}


    /**
     * 获取ViewModel
     */
    open fun <T : ViewModel> getViewModel(modelClass: Class<T>): T {
        return getViewModel(viewModelStore, modelClass)
    }

    open fun <T : ViewModel> getViewModel(store: ViewModelStore, modelClass: Class<T>): T {
        return ViewModelProvider(
            store,
            ViewModelProvider.AndroidViewModelFactory.getInstance(BaseApplication.context)
        ).get(modelClass)
    }

    open fun isFragmentNotActive(): Boolean {
        return (activity == null || activity?.isFinishing == true || activity?.isDestroyed == true || isDetached || !isAdded || isRemoving)
    }

    var title: String? = null

    private var errorDialog: CommonDialog? = null
    open fun showErrorDialog(
        msg: String,
        yes: String?,
        yesClickListener: View.OnClickListener,
        no: String? = null,
        noClickListener: View.OnClickListener? = null
    ) {
        if (errorDialog == null) {
            errorDialog = if (noClickListener == null) {
                DialogUtils.showCommonConfirmDialog(requireContext(), msg, yes) {
                    if (isFragmentNotActive()) return@showCommonConfirmDialog
                    errorDialog?.dismiss()
                    yesClickListener.onClick(it)
                }
            } else {
                DialogUtils.showCommonDialog(requireContext(), msg, yes, {
                    if (isFragmentNotActive()) return@showCommonDialog
                    errorDialog?.dismiss()
                    yesClickListener.onClick(it)
                }, no, {
                    if (isFragmentNotActive()) return@showCommonDialog
                    errorDialog?.dismiss()
                    noClickListener.onClick(it)
                }, false)
            }
        } else {
            if (noClickListener == null) {
                errorDialog?.showDialog(msg, yes, {
                    if (isFragmentNotActive()) return@showDialog
                    errorDialog?.dismiss()
                    yesClickListener.onClick(it)
                })
            } else {
                errorDialog?.showDialog(msg, yes, {
                    if (isFragmentNotActive()) return@showDialog
                    errorDialog?.dismiss()
                    yesClickListener.onClick(it)
                }, no, {
                    if (isFragmentNotActive()) return@showDialog
                    errorDialog?.dismiss()
                    noClickListener.onClick(it)
                })
            }
        }
        errorDialog?.show()
    }


    override fun onDestroyView() {
        errorDialog?.dismiss()
        errorDialog = null
        super.onDestroyView()
    }

    fun hideErrorDialog(){
        errorDialog?.dismiss()
    }
}